package com.hkj.configcenter.admin.controller;

import com.hkj.configcenter.admin.url.ServiceUrl;
import com.hkj.configcenter.core.Context;
import com.hkj.configcenter.core.processor.ZkProcessor;
import com.hkj.configcenter.core.utils.ZkUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.List;

@Slf4j
@Controller
public class ExportConfigController {

    @GetMapping(ServiceUrl.CONFIG_CENTER_EXPORT)
    public void exportConfig(HttpServletResponse response, String path) throws Exception {
        response.setContentType("application/octet-stream;charset=UTF-8");
        StringBuffer buffer = new StringBuffer();

        String[] parentPaths = path.split("/");
        String parentPath = "";
        for (int i = 1; i < parentPaths.length - 1; i++) {
            parentPath = parentPath + "/" + parentPaths[i];
            String data = ZkUtils.exportTextRemark(parentPath);
            buffer.append(parentPath).append("=").append(data).append("\n");
        }

        exportConfig(path, buffer);

        PrintWriter out = response.getWriter();
        out.write(buffer.toString());
        out.flush();
        out.close();
    }

    @GetMapping(ServiceUrl.CONFIG_CENTER_APPLICATION_PROPERTIES)
    public void exportApplicationProperties(HttpServletResponse response, String path) throws Exception {
        response.setContentType("application/octet-stream;charset=UTF-8");

        StringBuffer buffer = new StringBuffer();
        if (path.indexOf(Context.getCommonConfigPath()) < 0) {
            exportApplicationProperties(Context.getCommonConfigPath(), buffer);
        }

        exportApplicationProperties(path, buffer);

        PrintWriter out = response.getWriter();
        out.write(buffer.toString());
        out.flush();
        out.close();
    }

    private static void exportApplicationProperties(String parentPath, StringBuffer buffer) {
        if (parentPath.contains(Context.ONLINE_MACHINES)) {
            return;
        }

        List<String> children = Context.zkClient.getChildren(parentPath);
        if (children.size() > 0) {
            for (String child : children) {
                exportApplicationProperties(parentPath + "/" + child, buffer);
            }
            buffer.append("\n");
        } else {
            String[] value_desc = ZkUtils.readTextRemark(parentPath);
            value_desc[0] = value_desc[0].replaceAll("[\\n\\r]", "").trim();
            value_desc[1] = value_desc[1].replaceAll("[\\n\\r]", "\n# ");
            if (value_desc.length > 1) {
                buffer.append("# ").append(value_desc[1]).append("\n");
            }
            buffer.append(ZkProcessor.getKey(parentPath)).append("=").append(value_desc[0]).append("\n");
        }
    }

    public static void exportConfig(String path, StringBuffer buffer) {
        if (path.contains(Context.ONLINE_MACHINES)) {
            return;
        }

        String exportTextRemark = ZkUtils.exportTextRemark(path);
        buffer.append(path).append("=").append(exportTextRemark).append("\n");

        List<String> children = Context.zkClient.getChildren(path);
        if (children.size() > 0) {
            Collections.sort(children, Context.STRING_COMPARATOR);
            for (String child : children) {
                exportConfig(path + "/" + child, buffer);
            }
        }
    }

}
